﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SGRapidForm.Common;
using SGRapidForm.Permission.Entity;
using FishWebLib;
using System.Collections.Specialized;
using System.Data;

namespace SGRapidForm.Permission.Component
{
    public class PageComponent : ComponentBase
    {
        /// <summary>
        /// 添加一位新用户
        /// </summary>
        /// <param name="UserDetails">一位用户</param>
        /// <returns>是否添加成功</returns>
        public bool AddPage(PageEntity page)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Page_ADD", page) > 0);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pagename"></param>
        /// <returns></returns>
        public PageEntity Page_GetKeyidByPageName(string pagecode)
        {
            return FishBLLHelper.CallSpGetDataItem<PageEntity>("sgrf_permission_Page_GetKeyidByPageName", null, pagecode);
        }


         /// <summary>
        /// 删除一页面
        /// </summary>
        /// <param name="keyid">页面的Keyid</param>
        /// <returns>是否删除成功</returns>
        public bool Page_Delete(string keyid)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Page_Delete", null, keyid) > 0);
        }
        

        /// <summary>
        /// 批量删除用户
        /// </summary>
        /// <param name="keyids">keyids</param>
        /// <returns>批量删除是否成功</returns>
        public bool BulkDeletePage(string keyids)
        {
            //Keyid数量
            int count = SqlHelper.GetKeyidsCount(keyids);
            //Keyids转换成Sql语句
            keyids = SqlHelper.ConvertKeyidsToSqlStatement(keyids);

            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Page_BulkDelete", null, keyids) == count);
        }

        /// <summary>
        /// 根据keyid来查询一条记录
        /// </summary>
        /// <param name="keyid">keyid</param>
        /// <returns>一条记录的集合</returns>
        public PageEntity Page_GetByKeyid(string keyid)
        {
            return FishBLLHelper.CallSpGetDataItem<PageEntity>("sgrf_permission_Page_GetByKeyid", null, keyid);
        } 

        /// <summary>
        /// 根据modulekeyid来查询一条记录
        /// </summary>
        /// <param name="keyid">keyid</param>
        /// <returns>一条记录的集合</returns>
        public List<PageEntity> Page_GetByModuleKeyid(string keyid)
        {
            return FishBLLHelper.CallSpGetDataItemList<PageEntity>("sgrf_permission_Page_GetPageByModuleKeyid", null, keyid);
        }

        /// <summary>
        /// 更新一条记录
        /// </summary>
        /// <param name="UserDetails">一条记录</param>
        /// <returns>是否更新成功</returns>
        public bool UpdatePage(PageEntity page)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Page_Update", page) > 0);
        }

        /// <summary>
        /// 查询所有的页面信息
        /// </summary>
        /// <returns>页面信息集合</returns>
        public List<PageEntity> GetAllList()
        {
            return FishBLLHelper.CallSpGetDataItemList<PageEntity>("sgrf_permission_Page_GetAll", null);
        }

        /// <summary>
        /// 进行过滤、排序、分页查询用户
        /// </summary>
        /// <param name="startRecord">开始记录索引</param>
        /// <param name="maxRecords">每页记录数</param>
        /// <param name="sortColumns">排序列</param>
        /// <param name="filterConditions">过滤条件SQL</param>
        /// <returns>用户集合</returns>
        public List<PageEntity> PagingGetPages(Int32 startRecord, Int32 maxRecords, String sortColumns, String filterConditions)
        {
            VerifySortColumns(sortColumns);
            if (string.IsNullOrWhiteSpace(sortColumns))
            {
                sortColumns = "[order] ASC";
            }

            NameValueCollection fieldCollection = SqlHelper.ParseQueryString(filterConditions);

            StringBuilder sql = new StringBuilder();

            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "PageCode", DbType.String, "AND PageCode like '%{0}%'"));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "PageName", DbType.String, " AND PageName like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "ModuleKeyid", DbType.String, " AND ModuleKeyid = '{0}' "));
            filterConditions = sql.ToString();

            return FishBLLHelper.CallSpGetDataItemList<PageEntity>("sgrf_permission_Page_PagingGetPages", null, startRecord, maxRecords, sortColumns, filterConditions);
        }

        /// <summary>
        /// 验证排序参数是否包含非法字符，有则抛出异常
        /// </summary>
        /// <param name="sortColumns"></param>
        private void VerifySortColumns(string sortColumns)
        {
            sortColumns = sortColumns.ToLowerInvariant().Replace(" asc", String.Empty).Replace(" desc", String.Empty);
            String[] columnNames = sortColumns.Split(',');

            foreach (String columnName in columnNames)
            {

                switch (columnName.Trim().ToLowerInvariant())
                {
                    case "pagename":
                    case "pagecode":
                    case "pagenameen":
                    case "createdate":
                    case "order":
                    case "":
                        break;
                    default:
                        throw new ArgumentException("SortColumns contains an invalid column name.");
                }
            }
        }

        /// <summary>
        /// 进行过滤、排序、分页查询用户记录数
        /// </summary>
        /// <param name="filterConditions">过滤条件</param>
        /// <returns>记录数</returns>
        public int PagingGetPageCount(String filterConditions)
        {
            NameValueCollection fieldCollection = SqlHelper.ParseQueryString(filterConditions);

            StringBuilder sql = new StringBuilder();
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "PageCode", DbType.String, "AND PageCode like '%{0}%'"));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "PageName", DbType.String, " AND PageName like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "ModuleKeyid", DbType.String, " AND ModuleKeyid = '{0}' "));
            filterConditions = sql.ToString();

            return Convert.ToInt32(FishBLLHelper.CallSpExecuteScalar("sgrf_permission_Page_PagingGetPageCount", null, filterConditions));
        }

        /// <summary>
        /// 导入页面数据前清空
        /// </summary>
        /// <returns></returns>
        public bool Page_ClearBeforeImporting()
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Page_ClearBeforeImporting", null) > 0);
        }
    }
}
